/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include <QMessageBox>
#include <QHBoxLayout>

#include "navigator.h"


using namespace SQL;

Navigator::Navigator(QWidget *parent)
		: QWidget(parent),m_qry(0)
{
	QHBoxLayout *horizontalLayout=  new QHBoxLayout(this);

	m_adauga = new QPushButton(this);
	m_adauga->setText(tr("Adauga"));
	m_adauga->setFocusPolicy(Qt::NoFocus);
	horizontalLayout->addWidget(m_adauga);

	m_modifica = new QPushButton(this);
	m_modifica->setText(tr("Modifica"));
	m_modifica->setFocusPolicy(Qt::NoFocus);
	horizontalLayout->addWidget(m_modifica);

	m_sterge = new QPushButton(this);
	m_sterge->setText(tr("Sterge"));
	m_sterge->setFocusPolicy(Qt::NoFocus);
	horizontalLayout->addWidget(m_sterge);

	horizontalLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));

	m_aplica = new QPushButton(this);
	m_aplica->setText(tr("Aplica"));
	m_aplica->setFocusPolicy(Qt::NoFocus);
	horizontalLayout->addWidget(m_aplica);

	m_renunta = new QPushButton(this);
	m_renunta->setText(tr("Renunta"));
	m_renunta->setFocusPolicy(Qt::NoFocus);
	horizontalLayout->addWidget(m_renunta);

	connect(m_sterge,SIGNAL(clicked()),SLOT(sterg()));
}


Navigator::~Navigator()
{
}

void Navigator::sterg()
{
	if (!m_qry|| (m_deleteMessage.length() && QMessageBox::question(this,tr("Stergere"),m_deleteMessage,QMessageBox::Yes|QMessageBox::No)==QMessageBox::No))
		return;
	try
	{
		m_qry->remove();
	}
	catch(const QString & errorMsg)
	{
		QMessageBox::critical( this, tr("Eroare stergere"), errorMsg );
		m_qry->cancel();
	}
	catch(...)
	{
		QMessageBox::critical( this, tr("Eroare stergere"), tr("Eroare necunoscuta"));
		m_qry->cancel();
	}
	emit(sterge());
}

const QString & Navigator::queryName()
{
	return m_queryName;
}

void Navigator::setQueryName(const QString & queryName)
{
	m_queryName = queryName;
	setQuery(defaultDatabase()->query(m_queryName));
}

void Navigator::setQuery(Query * qry)
{
	m_qry=qry;

	if (!m_qry)
		return;

	connect(m_qry,SIGNAL(beforeInsert()),SLOT(disableActions()));
	connect(m_qry,SIGNAL(beforeUpdate()),SLOT(disableActions()));

	connect(m_qry,SIGNAL(afterPost()),SLOT(enableActions()));
	connect(m_qry,SIGNAL(afterCancel()),SLOT(enableActions()));

	connect(m_qry,SIGNAL(afterPost()),SIGNAL(aplica()));
	connect(m_qry,SIGNAL(afterCancel()),SIGNAL(renunta()));

	connect(m_adauga,SIGNAL(clicked()),m_qry,SLOT(insert()));
	connect(m_modifica,SIGNAL(clicked()),m_qry,SLOT(update()));

	connect(m_aplica,SIGNAL(clicked()),m_qry,SLOT(post()));
	connect(m_renunta,SIGNAL(clicked()),m_qry,SLOT(cancel()));

	connect(m_adauga,SIGNAL(clicked()),SIGNAL(adauga()));
	connect(m_modifica,SIGNAL(clicked()),SIGNAL(modifica()));
}

void Navigator::disableActions()
{
	m_adauga->setEnabled(false);
	m_modifica->setEnabled(false);
	m_sterge->setEnabled(false);
	m_aplica->setEnabled(true);
	m_renunta->setEnabled(true);
}

void Navigator::enableActions()
{
	m_adauga->setEnabled(true);
	m_modifica->setEnabled(true);
	m_sterge->setEnabled(true);
	m_aplica->setEnabled(false);
	m_renunta->setEnabled(false);
}

void Navigator::post()
{
	if (m_qry)
		m_qry->post();

}
void Navigator::cancel()
{
	if (m_qry)
		m_qry->cancel();
}

void Navigator::insert()
{
	m_qry->insert();
	emit(adauga());
}

void Navigator::update()
{
	m_qry->update();
	emit(modifica());
}

void Navigator::remove()
{
	sterg();
}

const QString & Navigator::deleteMessage()
{
	return m_deleteMessage;
}

void Navigator::setDeleteMessage(const QString & message)
{
	m_deleteMessage=message;
}

void Navigator::showEvent(QShowEvent * event)
{
	if (m_qry)
	{
		m_adauga->setEnabled(m_qry->canInsert());
		m_modifica->setEnabled(m_qry->canUpdate());
		m_sterge->setEnabled(m_qry->canDelete());
		m_aplica->setDisabled(m_qry->state()==Query::Select);
		m_renunta->setDisabled(m_qry->state()==Query::Select);
	}
	QWidget::showEvent(event);
}

